//公共方法
var Vue = require('vue');
var axios = require('axios');
axios.defaults.withCredentials = true;

function requestAjax(url,param,callBack,failBack=null) {
    axios.get(url,{
        params: param
    },)
    .then(function (response) {
        callBack(response);
    })
    .catch(function (error) {
        failBack(error);
    });
}

function postAjax(url,param,callBack,failBack=null) {
    axios.post(url, param)
    .then(function (response) {
        callBack(response);
    })
    .catch(function (error) {
        failBack(error);
    });
}

function dateFormate(num,type='date'){
    var d=new Date(num);
    var year=d.getFullYear();
    var day=d.getDate();
    var month=+d.getMonth()+1;
    var hour=d.getHours();
    var minute=d.getMinutes();
    var second=d.getSeconds();
    if(type=='date'){
        var f=year+"-"+formate(month)+"-"+formate(day)+" "+formate(hour)+":"+formate(minute)+":"+formate(second);
    }else if(type=='birth'){
        var f=year+"-"+formate(month)+"-"+formate(day)
    }
    
    function formate(d){
        return d>9?d:'0'+d;
    }
    return f;
}

//设置cookie1： 
function setCookie(name, value){
    let Days = 1,
    exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//读取cookie：
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");　　
    return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : null;
}

/**
 * 设置微信网页标题
 * @param {number | string } title 你要设置的标题
 */
function setPageTitle(title){
        document.title = title
        var mobile = navigator.userAgent.toLowerCase()
        if (/iphone|ipad|ipod/.test(mobile)) {
        var iframe = document.createElement('iframe')
        iframe.style.display = 'none'
        // 替换成站标favicon路径或者任意存在的较小的图片即可
        iframe.setAttribute('src', '/favicon.ico')
        var iframeCallback = function () {
        setTimeout(function () {
        iframe.removeEventListener('load', iframeCallback)
        document.body.removeChild(iframe)
        }, 0)
        }
        iframe.addEventListener('load', iframeCallback)
        document.body.appendChild(iframe)
        }
}
module.exports = {
  requestAjax,
  postAjax,
  dateFormate,
  setCookie,
  getCookie,
  setPageTitle
}
